﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Microsoft.SharePoint;
using System.Web.UI;
using System.Data.SqlClient;
using VPLink2013.Framework;
using System.Xml.Serialization;

namespace VPLink2013.Webcontrols
{
    public class EncartPersonnel: WebcontrolVPLink
    {
        private GlobalVPLink2013 global;
        private List<string> lstTitres = new List<string>();

        protected override void Render(HtmlTextWriter writer)
        {
            try
            {
                Template = "webcontrol EncartPersonnel";
                SetUrlPageCrt();

                global = new GlobalVPLink2013();
                global.Context = new Context();

                global.Context.Mode = "Home";
                if (!isDefaultPage)
                {
                    global.Context.Mode = "PageInterne";
                }
				global.Context.EncartVersion = "2013";
				if( encartPersoVersion == "VPLink2014" )
				{
					global.Context.EncartVersion = "2014";
				}
				global.Context.RechercheVersion = "2013";
				if( rechercheVersion == "VPLink2014" )
				{
					global.Context.RechercheVersion = "2014";
				}
                string login = VPLink2013XsltTransformHelper.StringAfter(this.Context.User.Identity.Name);
				strFileCacheUtilisateur = Path.Combine(pathCache, string.Format(@"Utilisateur\{0}\profil.xml", login));

                if (!FileSerialization.CheckFileCacheExists(strFileCacheUtilisateur, 10))
                {
                    SetUtilisateurCrt();
                }
                else
                {
                    UtilisateurCrt = FileSerialization.CreateObjFromXML<Utilisateur>(strFileCacheUtilisateur);
                }
                global.Utilisateur = UtilisateurCrt;

                string strXml = FileSerialization.CreateXmlStringFromObj(global);

				string strXslFileName = string.Empty;
				if( encartPersoVersion.Equals("VPLink2013", StringComparison.CurrentCultureIgnoreCase))
				{
					strXslFileName = "EncartPersonnel.xsl";
					if( photoEncartPerso == "Non" )
					{
						strXslFileName = "EncartPersonnelSansPhoto.xsl";
					}
				}
				else
				{
					if( encartPersoVersion.Equals("VPLink2014", StringComparison.CurrentCultureIgnoreCase))
					{
						strXslFileName = "EncartPersonnelVPLink2014.xsl";
					}
				}

                string strXslPath = Path.Combine(pathVP, "XSL", "EncartPersonnel", strXslFileName);
                
                ContextTrace.AddTrace("GetHtml.strXslPath = " + strXslPath);
                
                string strHtml = string.Empty;

                string strHtmlFileCache = pathCache + @"EncartPersonnel\EncartPersonnel_"
                    + login + "_" + global.Context.Mode + ".htm";

                strHtml = VPLink2013XsltTransformHelper.GetTransform(strXml, strXslPath, UtilisateurCrt.Login, pathTemp);
                FileSerialization.TraceFile(strHtmlFileCache, strHtml);
                sbHtml.AppendLine(strHtml);
            }
            catch (Exception exc)
            {
                ContextTrace.AddTrace("Erreur.EncartPersonnel.Render : " + exc.Message + "/" + exc.StackTrace);
            }
            base.Render(writer);
        }
    }
}
